package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Whether;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-18 15:51
 * @descreption 领导办理分配接口
 *
 * yuhh
 **/
@Component("distribution1.0")
public class DistributionService extends AbstractApiService {
    @Override
    public Map service(Map request) {
        /**
         * 1.得到领导分配的主办和协办人员
         * 2.更新表的经办人员和状态字段
         * 3.通知经办人员和协人员
         * 4.通知申报人项目已进入办理阶段*
         */
        Whether pass = whether(request);
        String mainManagerId = string(request, "mainManagerId");
        String otherManagerId = string(request, "otherManagerId");
        User mainManager = Mongo.buildMongo().eq("_id", mainManagerId).findOne(User.class);
        User otherManager = Mongo.buildMongo().eq("_id", otherManagerId).findOne(User.class);
        DeclareInfo declareInfo =Mongo.buildMongo().eq("_id", new ObjectId(id(request))).findOne(DeclareInfo.class);
        //更新数据
        Mongo.buildMongo().eq("_id", new ObjectId(id(request))).updateFirst(update -> {
            update.set("declareStatus", declareInfo.getDeclareStatus().handler(pass));
            update.set("mainManagerId", mainManager.getId());
            update.set("otherManagerId", otherManager.getId());
            update.set("updateAt", System.currentTimeMillis() / 1000);
        }, DeclareInfo.class);

        declareLog(id(request));
        return ApiResponse.success();

    }

}
